import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options


# 设置浏览器、启动浏览器
def create_driver():
    options = Options()
    options.add_argument('--no-sandbox')
    options.add_experimental_option("detach", True)
    driver_edge = webdriver.Edge(options=options)
    driver_edge.maximize_window()
    return driver_edge


# 夹具
@pytest.fixture(scope="module")
def driver():
    driver = create_driver()
    driver.get("http://course.recttech.cn/baggage-fee/")
    yield driver
    driver.quit()


class TestBaggageFee:
    def test_fee_01(self, driver):
        # 行李重量不超过30公斤
        driver.find_element(By.ID, "weight").send_keys("28")
        driver.find_element(By.CSS_SELECTOR, "button").click()
        fee = driver.find_element(By.ID, "fee").get_attribute("value")

        driver.find_element(By.CSS_SELECTOR, "button").click()

        assert fee == "0"

    def test_fee_02(self, driver):
        weight = driver.find_element(By.ID, "weight")
        weight.clear()
        weight.send_keys("32")

        # 乘客仓位：头等舱
        cabins = driver.find_elements(By.NAME, "cabin")
        cabins[0].click()
        # 乘客类型：国内乘客
        passenger_types = driver.find_elements(By.NAME, "passengerType")
        passenger_types[0].click()
        # 是否残疾：否
        disabled = driver.find_element(By.ID, "disabled")
        if disabled.is_selected():
            disabled.click()

        driver.find_element(By.CSS_SELECTOR, "button").click()

        fee = driver.find_element(By.ID, "fee").get_attribute("value")

        assert fee == "8"

    def test_fee_03(self, driver):
        weight = driver.find_element(By.ID, "weight")
        weight.clear()
        weight.send_keys("32")

        # 乘客仓位：头等舱
        cabins = driver.find_elements(By.NAME, "cabin")
        cabins[0].click()
        # 乘客类型：国内
        passenger_types = driver.find_elements(By.NAME, "passengerType")
        passenger_types[0].click()
        # 是否残疾：是
        disabled = driver.find_element(By.ID, "disabled")
        if not disabled.is_selected():
            disabled.click()

        driver.find_element(By.CSS_SELECTOR, "button").click()

        fee = driver.find_element(By.ID, "fee").get_attribute("value")

        assert fee == "4"

    def test_fee_04(self, driver):
        weight = driver.find_element(By.ID, "weight")
        weight.clear()
        weight.send_keys("32")

        # 乘客仓位：头等舱
        cabins = driver.find_elements(By.NAME, "cabin")
        cabins[0].click()
        # 乘客类型：国外
        passenger_types = driver.find_elements(By.NAME, "passengerType")
        passenger_types[1].click()
        # 是否残疾：否
        disabled = driver.find_element(By.ID, "disabled")
        if disabled.is_selected():
            disabled.click()

        driver.find_element(By.CSS_SELECTOR, "button").click()

        fee = driver.find_element(By.ID, "fee").get_attribute("value")

        assert fee == "16"

    def test_fee_05(self, driver):
        weight = driver.find_element(By.ID, "weight")
        weight.clear()
        weight.send_keys("32")

        # 乘客仓位：头等舱
        cabins = driver.find_elements(By.NAME, "cabin")
        cabins[0].click()
        # 乘客类型：国外乘客
        passenger_types = driver.find_elements(By.NAME, "passengerType")
        passenger_types[1].click()
        # 是否残疾：是
        disabled = driver.find_element(By.ID, "disabled")
        if not disabled.is_selected():
            disabled.click()

        driver.find_element(By.CSS_SELECTOR, "button").click()

        fee = driver.find_element(By.ID, "fee").get_attribute("value")

        assert fee == "8"

    def test_fee_06(self, driver):
        weight = driver.find_element(By.ID, "weight")
        weight.clear()
        weight.send_keys("32")

        # 乘客仓位：其他舱
        cabins = driver.find_elements(By.NAME, "cabin")
        cabins[1].click()
        # 乘客类型：国内乘客
        passenger_types = driver.find_elements(By.NAME, "passengerType")
        passenger_types[0].click()
        # 是否残疾：否
        disabled = driver.find_element(By.ID, "disabled")
        if disabled.is_selected():
            disabled.click()

        driver.find_element(By.CSS_SELECTOR, "button").click()

        fee = driver.find_element(By.ID, "fee").get_attribute("value")

        assert fee == "12"
